import json, codecs, time, os, emoji
import logging, copy

from django.views import View
from django.http.request import HttpRequest
from django.http import JsonResponse
from django.utils import timezone
from django.db.models import Q

from django.conf import settings

from .models import IdmiCuZuModel
from .models import IdmiCuZuResModel

from .utils import sentiment2intvalue
from .utils import cuzu_label2int_label


# Create your views here.



# 暂时不开发废弃这个类
class StoreIdmiCuZu(View):
    def post(self, request: HttpRequest):
        '''
        {'create_time', 'price',
        'update_time', 'url_temp',
        'cuzu_res', 'title',
        'carspec_name', 'title_en',
        'sourceId', 'customer_voice_en',
        'address', 'model_id',
        'image_url', 'series',
        'boughtdate', 'URL',
        'series_src', 'customer_voice', 'OEM'}
        '''
        msg = request.POST.get('msg', '')
        msg = json.loads(msg)

        create_time = msg.get('create_time', '')
        URL = msg.get('URL', '')
        address = msg.get('address', '')
        boughtdate = msg.get('boughtdate', '')
        carspec_name = msg.get('carspec_name', '')
        title_zh = msg.get('title_zh', '')
        customer_voice_zh = msg.get('customer_voice_zh', '')
        title_en = msg.get('title_en', '')
        customer_voice_en = msg.get('customer_voice_en', '')
        image_url = msg.get('image_url', '')
        price = msg.get('price', '')
        website = msg.get('website', '')
        url_temp = msg.get('url_temp', '')
        src_bought_date = msg.get('src_bought_date', '')
        series_id = msg.get('series_id', '')
        src_series_name = msg.get('src_series_name', '')
        BodyType = msg.get('BodyType', '')
        Brand = msg.get('Brand', '')
        BrandIndicator = msg.get('BrandIndicator', '')
        FuelType = msg.get('FuelType', '')
        Group = msg.get('Group', '')
        ModelName = msg.get('ModelName', '')
        OEM = msg.get('OEM', '')
        Segment = msg.get('Segment', '')
        fid = msg.get('fid', '')


        idmicuzu = IdmiCuZuModel.objects.create(
            create_time = create_time,
            URL=URL[:400],
            address=address,
            boughtdate=boughtdate,
            carspec_name=carspec_name,
            title_zh=title_zh[:500],
            customer_voice_zh=customer_voice_zh[:5000],
            title_en=title_en[:1000],
            customer_voice_en=customer_voice_en[:10000],
            image_url=image_url[:400],
            price=price[:20],
            website=website,
            url_temp=url_temp[:400],
            src_bought_date=src_bought_date[:50],
            series_id = series_id,
            src_series_name=src_series_name,
            BodyType=BodyType,
            Brand=Brand,
            BrandIndicator=BrandIndicator,
            FuelType=FuelType,
            Group=Group,
            ModelName=ModelName,
            OEM = OEM,
            Segment = Segment,
            fid = fid
        )

        cuzu_res_zh = msg.get('cuzu_res')

        for cuzu_index in range(len(cuzu_res_zh)):

            IdmiCuZuResModel.objects.create(
                cuzu=idmicuzu,
                cuzu_label_1_int_value=cuzu_res_zh[cuzu_index]['feel_1'],
                cuzu_label_2_int_value=cuzu_res_zh[cuzu_index]['feel_2'],
                content_list_zh=cuzu_res_zh[cuzu_index]['content_list_zh'],
                content_list_en=cuzu_res_zh[cuzu_index]['content_list_en'],
                sentiment=cuzu_res_zh[cuzu_index]['sentiment']
            )

        return JsonResponse({'status': 200})




